<?php
include_once "../../../wp-load.php";

if (!isset($_GET["target"])) {
    wp_die("target 파라메터가 누락되었습니다.");
}
$users = get_users();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta http-equiv="Content-Script-Type" content="text/javascript" />
        <meta http-equiv="Content-Style-Type" content="text/css" />
        <title>search</title>
        <style type="text/css">
            html, body{padding: 0; margin: 0; font-size: 12px; font-family: dotum;}
            body{padding: 5px;}
            .button, .submit input, .button-secondary {
                background: url("../images/white-grad.png") repeat-x scroll left top #F2F2F2;
                text-shadow: 0 1px 0 #FFFFFF;
            }
            .button, .button-secondary, .submit input, input[type="button"], input[type="submit"] {
                border-color: #BBBBBB;
                color: #464646;
            }
            .submit input, .button, input.button, .button-primary, input.button-primary, .button-secondary, input.button-secondary, .button-highlighted, input.button-highlighted, #postcustomstuff .submit input {
                -moz-box-sizing: content-box;
                border-radius: 11px 11px 11px 11px;
                border-style: solid;
                border-width: 1px;
                cursor: pointer;
                font-size: 11px !important;
                line-height: 13px;
                padding: 3px 8px;
                text-decoration: none;
            }
            #search-user-table{width: 100%;}
            #search-user-table thead th{padding: 3px; background-color: #EEEEEE;}
            #search-user-table tbody tr td{text-align: center;}
            #search-user-table tbody tr td.align-left{text-align: left;}
        </style>
        <?php
        wp_enqueue_script("jquery");
        print_head_scripts();
        ?>
        <script type="text/javascript">
            //<![CDATA[
            jQuery(document).ready(function($){
                //얼룩무늬...까다롭네-_-
                function zebra(){
                    $("#search-user-table tbody tr:visible").css({
                        backgroundColor: "#FFFFFF"
                    });
                    $("#search-user-table tbody tr:visible").filter(function(i){
                        return i % 2 == 1;
                    }).css({
                        backgroundColor: "#EEEEEE"
                    });
                }
                zebra();

                $("#check_cancel_btn").click(function(e){
                    self.parent.tb_remove();//thickbox 제거 함수 호출
                });

                //선택버튼 클릭시 부모document의 타겟 엘리먼트 value에 값 입력
                $("#check_ok_btn").click(function(e){
                    var logins = [];
                    logins = $.map($("#search-user-table input[name=user_login]:checked"), function(v){
                        return $(v).val();
                    });
                    if(logins.length == 0) return;
                    $("#<?php echo $_GET["target"]; ?>", top.document).val(logins.join());
                    self.parent.tb_remove();//thickbox 제거 함수 호출
                });
                
                //검색 키업 바인드
                $("#sv").bind("keyup", function(e){
                    var sv = $.trim($(this).val());
                    if(sv){
                        $("#search-user-table tbody tr").hide();
                        $("#search-user-table tbody tr td:contains(" + sv + ")").closest("tr").show();
                    }else{
                        $("#search-user-table tbody tr").show();
                    }
                    zebra();
                });
            });
            //]]>
        </script>
    </head>
    <body>
        검색 <input type="search" id="sv" name="sv" value="" />
        <br />
        <br />
        <button type="button" id="check_ok_btn" class="button">선택</button>
        <button type="button" id="check_cancel_btn" class="button">취소</button>
        <table id="search-user-table">
            <thead>
                <tr>
                    <th style="width: 50px;">선택</th>
                    <th>아이디</th>
                    <th>이름</th>
                    <th>권한</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($users as $k): ?>
                <?php
                    $user = get_userdata($k->ID);
                    $varName = $table_prefix . "user_level";
                    if ($user->$varName == 10) {//관리자
                        continue;
                    }
                ?>
                <tr>
                    <td>
                        <input type="checkbox" name="user_login" value="<?php echo $user->user_login; ?>" />
                    </td>
                    <td class="align-left">
                        <?php echo get_avatar($user->ID, 32, "", $user->last_name . $user->first_name); ?>
                        <?php echo $user->user_login; ?>
                    </td>
                    <td><?php echo $user->last_name; ?><?php echo $user->first_name; ?></td>
                    <td>
                        <?php
                            $u_role = $user->wp_capabilities;
                            $u_keys = array_keys($u_role);
                            echo translate_user_role(ucfirst($u_keys[0]));
                        ?>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </body>
</html>
